package rule

// deprecated placeholder
import (
	"net"
	"strings"
)

// IsExactIp 判断是否为单一 IPv4 (不包含 / 或 -)
func IsExactIp(raw string) bool {
	if raw == "" {
		return false
	}
	if strings.Contains(raw, "/") || strings.Contains(raw, "-") {
		return false
	}
	ip := net.ParseIP(strings.TrimSpace(raw))
	return ip != nil && ip.To4() != nil
}

// IsCIDRIp 判断是否为合法 CIDR 表达式 (x.x.x.x/nn)
func IsCIDRIp(s string) bool {
	if !strings.Contains(s, "/") {
		return false
	}
	_, _, err := net.ParseCIDR(strings.TrimSpace(s))
	return err == nil
}

// IsRangeIp 判断是否为合法 IPv4 区间: start-end (含两端, start<=end)
func IsRangeIp(s string) bool {
	if !strings.Contains(s, "-") {
		return false
	}
	parts := SplitAndTrim(s, "-")
	if len(parts) != 2 {
		return false
	}
	start := net.ParseIP(parts[0])
	end := net.ParseIP(parts[1])
	if start == nil || end == nil || start.To4() == nil || end.To4() == nil {
		return false
	}
	return IPv4ToUint32(start) <= IPv4ToUint32(end)
}

// DetectKind 返回字符串对应的类别: exact / cidr / range (都不是则空字符串)
func DetectKind(raw string) string {
	switch {
	case IsExactIp(raw):
		return "exact"
	case IsCIDRIp(raw):
		return "cidr"
	case IsRangeIp(raw):
		return "range"
	default:
		return ""
	}
}
